var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notPayBillList:[],
    mrRoom:null,
    totalFee:0,
    billIdStr:"",
    idCommunity:"",
    idRoom:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      idCommunity: wx.getStorageSync("idCommunity")
    })

    if (options.idCommunity!=undefined){
      console.log(options);
      
      this.setData({
        idCommunity: options.idCommunity,
        idRoom: options.idRoom,
      })
    }
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.init(this)

    app.api.client_community_getMrRoom({
      token: wx.getStorageSync("token"),
      idCommunity: this.data.idCommunity,
    }, (res) => {
      if (res.data != undefined) {
        this.setData({
          mrRoom: res.data
        })
        
        app.api.client_propertyBill_getNotPayByRoom({
          token: wx.getStorageSync("token"),
          idRoom: res.data.idRoom
        }, (result) => {
          if (result.data.length > 0) {
            var totalFee = 0;
            var billIdArr = [];
            var billIdStr = "";
            for (var index in result.data) {
              console.log(result.data[index]);
              totalFee += result.data[index].talFee;
              billIdArr[index] = result.data[index].id;

              result.data[index].billOverdueTime = result.data[index].billOverdueTime.replace(/-/g, "/");
              result.data[index].billTime = result.data[index].billTime.replace(/-/g, "/");
              result.data[index].namePropertyBill = result.data[index].namePropertyBill.replace(/-/g, "/");
              result.data[index].talFee = result.data[index].talFee.toFixed(2);

              result.data[index].check = false;
              if(index == 0){
                result.data[index].check = true;
              }

            }
            this.setData({
              notPayBillList: result.data,
              totalFee: totalFee.toFixed(2),
              billIdStr: result.data[0].id,
            });
          }
        })


      }
    })

    


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  check: function (e) {
    let billIdStr = [];
    let data = this.data.notPayBillList;
    let index = e.currentTarget.dataset.index;
    console.log(index, data[index].check);

    if (data[index].check) {
      if (index == 0) {
        data[index].check = true;
      } else {
        data[index].check = false;
      }
    } else {
      if (data[index - 1].check == false) {
        app.toast('请结清上一个月的账单');
      } else {
        data[index].check = true;
      }
    }

    for (let i in data) {
      if (data[i].check) {
        billIdStr.push(data[i].id)
      }
    }


    this.setData({
      notPayBillList: data,
      billIdStr: billIdStr.join(",")
    });


  },



})